package com.sunl.mall.ali.mq.springboot.consumer;

import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.shade.com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.shade.com.alibaba.fastjson.TypeReference;
import com.aliyun.openservices.shade.org.apache.commons.lang3.SerializationException;
import com.sunl.mall.ali.mq.api.consumer.RocketMqMessageListener;
import com.sunl.mall.ali.mq.autoconfigure.annotation.MqConsumer;
import com.sunl.mall.ali.mq.model.RocketMqMessageBody;
import com.sunl.mall.ali.mq.model.RocketMqMessageContext;
import com.sunl.mall.common.api.topic.MqTopic;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Type;

@Slf4j
@MqConsumer(group = "firstConsumer")// 对应他的配置
public class AListener implements RocketMqMessageListener<String> {
    private final Type TYPE = new TypeReference<RocketMqMessageBody<String>>() {
    }.getType();

    @Override
    public Action call(RocketMqMessageBody<String> body, RocketMqMessageContext context) throws Exception {
        log.info("接收到消息 body:[{}] topic:[{}]", body.getContent(), context.getTopic());
        return Action.CommitMessage;
    }

    @Override
    public MqTopic subscribe() {
        return MqTopic.COMMON_MSG;
    }

    @Override
    public RocketMqMessageBody<String> deserialize(byte[] bytes, RocketMqMessageContext paramMessageContext) throws SerializationException {
        return JSONObject.parseObject(bytes, TYPE);
    }
}

